<?php 
include_once 'head.php';
include_once 'classes/UserHandler.class.php';

if (!$user->isAdmin()) {
    header('Location:index.php');
    exit;
}


$userHandler = new UserHandler();

if( $_SERVER['REQUEST_METHOD'] == 'POST'){
    
    if (isset($_POST['form'])) {
        $form = $_POST['form'];
        if ($form == 'pending') {            
            $ids = $_POST['accountids'];

            if (isset($_POST['approve'])) {
                $userHandler->changeStatus($ids, AuthUser::STATUS_ENABLED);
            }
            if (isset($_POST['reject'])) {
                $userHandler->changeStatus($ids, AuthUser::STATUS_REJECTED);
            }
            
        }
        
        if ($form == 'account') {
            $ids = $_POST['accountids'];
            if (isset($_POST['disable'])) {
                $userHandler->changeStatus($ids, AuthUser::STATUS_DISABLED);
            }
            if (isset($_POST['enable'])) {
                $userHandler->changeStatus($ids, AuthUser::STATUS_ENABLED);
            }
            if (isset($_POST['delete'])) {
                $userHandler->changeStatus($ids, AuthUser::STATUS_DELETED);
            }
            if (isset($_POST['admin'])) {
                $userHandler->changeUserRole($ids, UserRole::ADMIN);
            }   
            if (isset($_POST['data-entry'])) {
                $userHandler->changeUserRole($ids, UserRole::DATA_ENTRY_OPERATOR);
            }             
        }
    }
    
} 

// Get list of accounts
$accountsPendingApproval = $userHandler->getUsersByStatus(array(AuthUser::STATUS_PENDING_APPROVAL));

$enabledAccounts = $userHandler->getUsersByStatus(array(AuthUser::STATUS_ENABLED, AuthUser::STATUS_DISABLED));


if (isset($_SESSION['admin_message'])) {
    $adminMessage = $_SESSION['admin_message'];
    unset($_SESSION['admin_message']);
?>

<div class="register_success_message"><?php echo $adminMessage;?></div>

<?php } ?>


<br /><br />

 <img title="chims@lrh" alt="chims@lrh" src="img/chims.png"></img><br /><br /><br />
 
<!-- registration requests -->
  <form action="admin.php" method = "post" id="pending-form">
      <input type="hidden" name="form" value="pending"/>
<table class="tg">
  <tr><th class="thg">Registration Requests:</th></tr>   
  <tr><td class="tdw">

    <table id="pending-approval">
        <thead>
            <th></th>
            <th>Login Name</th>
            <th>Title</th>
            <th>First Name</th>
            <th>Surname</th>
            <th>Unit</th>
            <th>Designation</th>
            <th>NID No</th>
            <th>Email</th>
            <th>Mobile</th>
            <th>Other No</th>
        </thead>
        <tbody>
<?php foreach ($accountsPendingApproval as $user) { ?>
        <tr>
            <td><input type="checkbox" name="accountids[]" value="<?php echo $user->getUserId();?>"/></td>
            <td><?php echo $user->getUserName();?></td>
            <td><?php echo $user->getTitle();?></td>
            <td><?php echo $user->getFirstName();?></td>
            <td><?php echo $user->getLastName();?></td>
            <td><?php echo $user->getUnit();?></td>
            <td><?php echo $user->getDesignation();?></td>
            <td><?php echo $user->getNid();?></td>
            <td><?php echo $user->getEmail();?></td>
            <td><?php echo $user->getMobile();?></td>
            <td><?php echo $user->getOtherPhone();?></td>            
        </tr>
<?php } ?>
        </tbody>
    </table>
  
      </td>
  </tr>
  <tr>
      <td class="tdw">
          <input class="button" type="submit" value="Approve" id="approvebtn" name="approve"/>
          <input class="button" type="submit" value="Reject" id="rejectbtn" name="reject"/>
      </td>
  </tr>
</table>
 </form>

<!-- accounts -->
<?php
$statusArray = array(
    AuthUser::STATUS_PENDING_APPROVAL => 'Pending Approval',
    AuthUser::STATUS_ENABLED => 'Enabled',
    AuthUser::STATUS_DISABLED => 'Disabled',
    AuthUser::STATUS_REJECTED => 'Rejected',
    AuthUser::STATUS_DELETED => 'Deleted'    
);
?>
<form action="admin.php" method = "post" id="account-form">
    <input type="hidden" name="form" value="account"/>
<table class="tg">
  <tr><th class="thg">User Accounts:</th></tr>   
  <tr><td class="tdw">
    <table id="account-list">
        <thead>
            <th></th>
            <th>Login Name</th>
            <th>Title</th>
            <th>First Name</th>
            <th>Surname</th>
            <th>Unit</th>
            <th>Designation</th>
            <th>NID No</th>
            <th>Email</th>
            <th>Mobile</th>
            <th>Other No</th>
            <th>Type</th>
            <th>Status</th>
        </thead>
        <tbody>
<?php foreach ($enabledAccounts as $user) { ?>
        <tr>
            <td><input type="checkbox" name="accountids[]" value="<?php echo $user->getUserId();?>"/></td>
            <td><?php echo $user->getUserName();?></td>
            <td><?php echo $user->getTitle();?></td>
            <td><?php echo $user->getFirstName();?></td>
            <td><?php echo $user->getLastName();?></td>
            <td><?php echo $user->getUnit();?></td>
            <td><?php echo $user->getDesignation();?></td>
            <td><?php echo $user->getNid();?></td>
            <td><?php echo $user->getEmail();?></td>
            <td><?php echo $user->getMobile();?></td>
            <td><?php echo $user->getOtherPhone();?></td>            
            <td><?php echo $user->getUserRoleId() == 1 ? 'Admin' : 'Data Entry';?></td>   
            <td><?php echo $statusArray[$user->getStatus()];?></td>   
        </tr>
<?php } ?>
        </tbody>
    </table>
      </td>
  </tr>
  <tr>
      <td class="tdw">
          <input class="button" type="submit" value="Disable" id="disablebtn" name="disable"/>
          <input class="button" type="submit" value="Enable" id="enablebtn" name="enable"/>
          <input class="button" type="submit" value="Delete" id="deletebtn" name="delete"/>
          <input class="button" type="submit" value="Change to Admin" id="adminbtn" name="admin"/>
          <input class="button" type="submit" value="Change to Data Entry" id="data-entrybtn" name="data-entry"/>
      </td>
  </tr>  
</table>
  </form>


<link href="assets/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="assets/js/jquery.textbox-hinter.min.js"></script>  
<script language="javascript" src="assets/js/jquery.dataTables.min.js"></script>
<script>
        
$(document).ready(function(){
    $('#pending-approval').dataTable({
       "bSort": false,
       "bPaginate": false
      });
    $('#account-list').dataTable({
       "bSort": false,
       "bPaginate": false
      });            
	
    $("#approvebtn").click(function(){
        
        var checked = $('#pending-form input:checked').length;

        if (checked == 0) {
            alert('please select an item');
        } else {
            $('#pending-form').submit();
        }
    });
    $("#rejdectbtn").click(function(){
        
    });
    $("#disablebtn").click(function(){
        
    });
    $("#enablebtn").click(function(){
        
    });
    $("#adminbtn").click(function(){
        
    });
    $("#data-entrybtn").click(function(){
        
    });
              
                  
});
</script>
        
<?php include_once 'foot.php'; ?>